package com.ujiuye.controller;

import com.ujiuye.domain.po.Actor;
import com.ujiuye.domain.po.Movie;
import com.ujiuye.service.IActorService;
import com.ujiuye.service.IMovieAndActorService;
import com.ujiuye.service.IMovieService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("movieAndActor")
public class MovieAndActorController {

    @Autowired
    private IMovieAndActorService movieAndActorService;

    @Autowired
    private IMovieService movieService;

    @Autowired
    private IActorService actorService;

    @GetMapping("/findAll")
    public Map<String, Object> findAllMovieAndActor() {
        List<Movie> movieList = movieService.findAll();
        List<Actor> actorList = actorService.findAll();
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("movieList", movieList);
        resultMap.put("actorList", actorList);
        return resultMap;
    }

    // 删除电影与演员
    @PostMapping("/delete")
    public String delete(@RequestBody Movie movie) {
        movieAndActorService.deleteByMovieId(movie.getId());
        return "删除成功";
    }

    // 添加电影与演员
    @PostMapping("/update")
    public String update(@RequestBody Movie movie) {
        movieAndActorService.update(movie);
        return "提交成功";
    }

    @GetMapping("/edit")
    public Map<String, Object> editMovieAndActor(int movieId) {

        /*
        1.2 接口的逻辑:
            根据电影的id查询出电影的数据: movie
            查询所有的电影: movieList
            查询出所有的演员: actorList
        */
        // 根据电影的id查询出电影的数据: movie
        Movie movie = movieService.findById(movieId);
        /*// 查询所有的电影: movieList
        List<Movie> movieList = movieService.findAll();
        // 查询出所有的演员: actorList
        List<Actor> actorList = actorService.findAll();*/
        Map<String, Object> resultMap = findAllMovieAndActor();

        /*1.3 返回的数据
        以上三个数据:
        movie, movieList, actorList
        定义一个map封装这三个数据,返回map*/
        /*Map<String, Object> map = new HashMap<>();
        map.put("movie", movie);
        map.put("movieList", movieList);
        map.put("actorList", actorList);*/
        resultMap.put("movie", movie);

        return resultMap;

    }

    // 添加电影与演员
    @PostMapping("/save")
    public String save(@RequestBody Movie movie) {
        System.out.println(" movie ==== " + movie);
//        System.out.println(" actorIds ==== " + actorIds);
        movieAndActorService.save(movie);
        return "提交成功";
    }
}
